package Sql_package;

/**
 *
 * @author lemarcha
 */


import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class sgbd
{

Connection connexion;
ResultSet results;
ResultSetMetaData rsmd;
DatabaseMetaData dma;
String catalog;
String types[];
Statement statement;
static Statement stmt;




//----------------------------
public void ConnexionDatabase(String url,String login,String password) throws InstantiationException{


            //Chargement du driver hsqldb
            //connexion = DriverManager.getConnection(url,login,password);

            try {

                Class.forName("org.hsqldb.jdbcDriver").newInstance();

            } catch (Exception e) {
                System.out.println("Erreur lors du chargement du driver:" + e.getMessage());
            }
            
            // Connexion a  la base de donnees  

            //Connection connexion = DriverManager.getConnection("jdbc:hsqldb:file:database", "sa", "");
            try {
                connexion = DriverManager.getConnection(url, login, password);
            } catch (SQLException ex) {
                Logger.getLogger(sgbd.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                dma = connexion.getMetaData();
/*
                /results est une enumeration -- mettre import java.util.*
                results = new ResultSet(dma.getCatalogs());
                String s[];
                while(results.hasMoreElements()) {
                s = results.NextElement();
                }
                types = new String[1];
                types[0] = "TABLES";
                } catch (SQLException ex) {
                Logger.getLogger(sgbd.class.getName()).log(Level.SEVERE, null, ex);
                }
                 */
            } catch (SQLException ex) {
                Logger.getLogger(sgbd.class.getName()).log(Level.SEVERE, null, ex);
            }

/*
              results = new ResultSet(dma.getCatalogs());

  String s[];

  while(results.hasMoreElements()) {
   s = results.NextElement();
  }


            types = new String[1];
            types[0] = "TABLES";
        } catch (SQLException ex) {
            Logger.getLogger(sgbd.class.getName()).log(Level.SEVERE, null, ex);
        }

*/
}



//-----------------
    public void Close(){

        // Fermeture -- Deconnexion de la base de donnees

        try{

            statement = connexion.createStatement();
            statement.executeQuery("SHUTDOWN");
            statement.close();
            connexion.close();

         }
         catch(Exception e){
          System.out.println("echec lors de la fermeture:"+e.getMessage());

         }
    }


//-----------------------------
/*public String[] getTableNames()
{

 String[] tbnames = null;

 Vector tname = new Vector();

 try{
  results = new resultSet(dma.getTables(catalog,null,"%",types));

  while (results.hasMoreElements())
   tname.addElement(results.getColumnValue("TABLE_NAME"));
 }
 catch(Exception e){
  System.out.println(e.getMessage());

 }
 tbnames = new String[tname.size()];

 for(int i=0;i<tname.size();i++)
  tbnames[i] = (String)tname.elementAt(i);

 return tbnames;

}*/

//--------------------------------
/*public String[] getTableMetaData()
{

 results = null;

 try{
  results = new resultSet(dma.getTables(catalog,null,"%",types));

 }
 catch(Exception e){
  System.out.println(e.getMessage());

 }
 return results.getMetaData();

}*/

//------------------------------------------------
/*public String[] getColumnMetaData(String tablename)
{

 results = null;

 try{
  results = new resultSet(dma.getTables(catalog,null,tablename,null));

 }
 catch(Exception e){
  System.out.println(e.getMessage());

 }
 return results.getMetaData();

}*/

//------------------------------------------------
/*public String[] getColumnNames(String table)
{

 String[] tbnames = null;

 Vector tname = new Vector();

 try{
  results = new resultSet(dma.getTables(catalog,null,table,null));

  while (results.hasMoreElements())
   tname.addElement(results.getColumnValue("COLUMN_NAME"));
 }
 catch(Exception e){
  System.out.println(e.getMessage());

 }
 tbnames = new String[tname.size()];

 for(int i=0;i<tname.size();i++)
  tbnames[i] = (String)tname.elementAt(i);

 return tbnames;

}*/

//------------------------------------------------
/*public void getColumnValue(String table, String columnName)
{

 try{
  if (table.length()>0)
   results = Execute("Select "+columnName+" from "+table+" order by "+columnName);

 }
 catch(Exception e){
  System.out.println("Erreur sur la valeur de la colonne "
					 +columnName+e.getMessage());

 }
}*/

//------------------------------------------------
/*public String getNextValue(String columnName)
{

 String res = "";

 try{
  if (results.hasMoreElements())
   res = results.getColumnvalue(columnName);

 }
 catch(Exception e){
  System.out.println("Erreur sur la valeur suivante "+columnName+e.getMessage());

 }
 return res;

}*/

//----------------------AJOUT PARAMETRE DANS BDD--------------------

    /*public void ajouter(String sql_requete)
	{
		//deja connecter  --  Connection con = ConnexionDatabase();
		String createString;
		createString = "create table Employees (" +
							"Employee_ID INTEGER, " +
							"Name VARCHAR(30))";
		try {
			stmt = connexion.createStatement();
	   		stmt.executeUpdate(createString);
			stmt.close();
			connexion.close();

		} catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}
		JOptionPane.showMessageDialog(null,"Employees Table Created");
	}*/


//------------------------------------------------
    public ResultSet requete (String sql_requete){
            try {
                // recuperation de resultat
                Statement statement = null;
                ResultSet my_result_requete;
                results = null;

                try {
                        statement = connexion.createStatement();

                } catch (SQLException ex) {
                    Logger.getLogger(sgbd.class.getName()).log(Level.SEVERE, null, ex);
                }
    //ResultSet resultat = statement.executeQuery("SELECT * FROM table WHERE condition");
    // requete permettant de recuperer toutes les questions d une seance precise
                my_result_requete = statement.executeQuery(sql_requete);

                my_result_requete = statement.executeQuery("SELECT * FROM Questions WHERE ID_S=ID_Q");
    //  results = new ResultSet(statement.executeQuery(sql_requete));
    /*
                // requete permettant de recupererr les differentes reponses pour une question precise
                ResultSet resultat = statement.executeQuery("SELECT reponse_str FROM Reponse WHERE ID_Question=Id_Q")
                 */

            } catch (SQLException ex) {
                Logger.getLogger(sgbd.class.getName()).log(Level.SEVERE, null, ex);
            }
    return results;
            
    }

/*De la même façon, un nouvel objet resultSet (avec un r minuscule) peut être intéressant
 s'il permet de façon transparente de retourner automatiquement les résultats sous forme d'un
tableau de valeurs, ainsi qu'en encapsulant le nombre et le nom des colonnes contenues dans l'objet resultSet...
 * */

}